package com.example.fw;

import java.util.ArrayList;
import java.util.List;

import org.netbeans.jemmy.operators.JButtonOperator;
import org.netbeans.jemmy.operators.JDialogOperator;
import org.netbeans.jemmy.operators.JTextFieldOperator;
import org.netbeans.jemmy.operators.JTreeOperator;


public class FolderHelper extends HelpersBase {

	public FolderHelper(ApplicationManager applicationManager) {
		super (applicationManager);
		}
	
	public Folders getFolders() {
		List<String> list =  new ArrayList<String>();
		JTreeOperator tree = new JTreeOperator(mainFrame);
		Object[] children = tree.getChildren(tree.getRoot());
		for (Object child : children) {
			list.add(""+child);
			
		}
		return new Folders(list);
	}

	public String createFolder(String folderName) {
		manager.getMenuHelper().pushCreateFolder();
		JDialogOperator dialog = new JDialogOperator(mainFrame);
		new JTextFieldOperator(dialog).setText(folderName);
		new JButtonOperator (dialog, "OK").push();
		return waitMessegeDialog("Warning", 3000);
	}

	public String deleteFolder() {
		
		manager.getTreeHelper().chooseGroup();
		manager.getMenuHelper().pushDeleteFolder();
		JDialogOperator dialog = new JDialogOperator(mainFrame);
		new JButtonOperator (dialog, "Yes").push();
		return waitMessegeDialog("Warning", 3000);
	}
	
   public void removeFolder(String folderName) {
	
	   JTreeOperator tree = new JTreeOperator(mainFrame);
        int rowCount = tree.getRowCount();
        for (int i = 0; i < rowCount; i++) {
            String path = tree.getPathForRow(i).toString();
            if (path.equals("[jAddressBook, " + folderName + "]")) {
                tree.selectRow(i);
                manager.getMenuHelper().pushDeleteFolder();
                System.out.println("Wait to push delete");
            	
                JDialogOperator dialog = new JDialogOperator(mainFrame);                
                new JButtonOperator (dialog, "Yes").push();
                return;
            }
        }
        throw new Error("No folders to remove");
    }
	
	
	public String readNameOfFolder() {
		return manager.getTreeHelper().readNameOfFolder();
	}
	
	}
	
